<?php
class My_View_Helper_Sub {
	public function sub($Str, $Length, $postfix = '...') {
		
		$i = 0;
		$l = 0;
		$ll = strlen ( $Str );
		$s = $Str;
		$f = true;
		
		while ( $i <= $ll ) {
			if (ord ( $Str {$i} ) < 0x80) {
				$l ++;
				$i ++;
			} else if (ord ( $Str {$i} ) < 0xe0) {
				$l ++;
				$i += 2;
			} else if (ord ( $Str {$i} ) < 0xf0) {
				$l += 2;
				$i += 3;
			} else if (ord ( $Str {$i} ) < 0xf8) {
				$l += 1;
				$i += 4;
			} else if (ord ( $Str {$i} ) < 0xfc) {
				$l += 1;
				$i += 5;
			} else if (ord ( $Str {$i} ) < 0xfe) {
				$l += 1;
				$i += 6;
			}
			
			if (($l >= $Length - 1) && $f) {
				$s = substr ( $Str, 0, $i );
				$f = false;
			}
			
			if (($l > $Length) && ($i < $ll)) {
				$s = $s . $postfix;
				break;
			}
		}
		return $s;
	}
}
?>